/**
 * File:
 * Youth is just a unoptimized code!
 * -------------------------------------------------------------------------
 * Created by BigQ on 2016/9/20.
 *--------------------------------------------------------------------------
 * This technical information is confidential information,
 * without my written permission is prohibited for commercial purposes.　
 *--------------------------------------------------------------------------
 */
function bindSelectList(item, childItem) {
    $(item).click(function () {
        toggleSelect(item);
        if (childItem) {
            if ($(childItem).children(".selectshow").children(".selecthide").is(":visible")) {
                toggleSelect(childItem);
            }
        }
    });
    chooseSelectItem(item, childItem);
    if (childItem) {
        $(childItem).click(function () {
            toggleSelect(childItem);
            if ($(item).children(".selectshow").children(".selecthide").is(":visible")) {
                toggleSelect(item);
            }
        });
        chooseSelectItem(childItem);

    }
}
function toggleSelect(item) {
    $(item).children(".selectshow").children(".selecthide").toggle();
    $(item).toggleClass("blue");
    $(item).find("i").toggleClass("blue");
}
function chooseSelectItem(item, childItem) {
    $(item).find("li").click(function () {
        var valText = $(this).text();
        var val = $(this).attr('data');
        var oldVal = $(item).children("input").val();
        if (oldVal != val) {
            $(item).find("i").text(valText);
            $(item).children("input").val(val);
            if (childItem) {
                $.getJSON('../area/list', 'pid=' + val, function (data) {
                    if (data.status == 1) {
                        var childObj = $(childItem);
                        childObj.find("i").text('请选择...');
                        childObj.children("input").val('');
                        childObj.find("ul").html('');
                        $.each(data.data, function (index, elm) {
                            childObj.find("ul").append('<li data="' + elm.id + '">' + elm.name + '</li>');
                        });
                        chooseSelectItem(childItem);
                    }
                });
            }
        }
    });
}
function validPhone(phone) {
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if (!phone || !myreg.test(phone)) {
        return false;
    }
    else {
        return true;
    }
}
//短信验证码倒计时函数
var util = {
    wait: 30,
    hsTime: function (that) {
        _this = this;
        if (_this.wait == 0) {
            $(that).removeClass("on");
            $(that).removeAttr("disabled").val('重发验证码');
            _this.wait = 30;

        } else {
            var _this = this;
            $(that).attr("disabled", true).val(_this.wait + '(秒)后重发');
            _this.wait--;
            setTimeout(function () {
                _this.hsTime(that);
            }, 1000)
        }

    }
};
function getSmCode(selector) {
    $(selector).click(function () {
        var phone = $("#mobile").val();
        var vcode = $("#vcode").val();
        if (!validPhone(phone)) {
            waring("请正确填写手机号！")
            return false;
        }
        if (!vcode) {
            waring("请正确填写图形验证码！")
            return false;
        }
        $(this).addClass("on");
        util.hsTime(selector);
        $.ajax({
            type: "post",
            async: true,
            url: "../user/sendsmcode",
            dataType: "json",
            data: {mobile: '' + phone + '', vcode: '' + vcode + '',_csrf:''+$('#_csrf').val()+''},
            success: function (data) {
                if (data) {
                    waring(data.message);
                    if(data.data == 1){
                        util.wait=0;
                    }
                }

            },
            error: function (status) {
                util.wait=0;
                waring('抱歉！发送失败！请稍后重试！');
            }
        });
    });
}
function regSubmit() {
    $("#submit").click(function () {
        var phone = $("#mobile").val();
        var password = $("#password").val();
        var vcode = $("#vcode").val();
        var smcode = $("#smcode").val();
        var orgname = $("#orgname").val();
        var province = $("#province").val();
        var city = $("#city").val();
        var linkman = $("#linkman").val();
        var check_val = $("input:checkbox[name='check']:checked").val();
        if (!check_val) {
            waring("您没同意《最终用户协议》！");
            return false;
        }
        if (!phone || !password || !vcode || !smcode || !orgname || !linkman || !province || !city) {
            waring("请完整填写信息！");
            return false;
        }
        if (!validPhone(phone)) {
            waring("请正确填写手机号！")
            return false;
        }
        if (orgname.length < 4 || orgname.length > 100) {
            waring("企业/组织名长度需为4-100位")
            return false;
        }
        if (password.length < 6) {
            waring("密码长度不能小于6位")
            return false;
        }
        $(this).attr("disabled", "disabled");
        $(this).val('提交中...');
        $.ajax({
            type: "post",
            async: true,
            url: "../user/reguser",
            dataType: "json",
            data:$("#dataForm").serialize(),
            success: function (data) {
                if (data.status == 1) {
                    waring(data.message,function () {
                        window.location.href = 'login';
                    });
                }
                else{
                    waring(data.message);
                    $("#submit").removeAttr("disabled");
                    $("#submit").val('注册');
                }

            },
            error: function (status) {
                waring('抱歉！注册失败！请稍后重试！');
                $("#submit").removeAttr("disabled");
                $("#submit").val('注册');
            }
        });
        return false;
    });

}
//提交判断
